import scrapy
from douban.items import DoubanItem


class DianyingSpider(scrapy.Spider):
    name = 'dianying'
    allowed_domains = ['movie.douban.com']
    start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        list_all = response.xpath('//div[@class="item"]')
        for li in list_all:
            item = DoubanItem()
            item['name'] = li.xpath('.//span[@class="title"][1]/text()').extract_first()
            item['star'] = li.xpath('.//span[@class="rating_num"]/text()').extract_first()
            item['people'] = li.xpath('.//div[@class="star"]/span[4]/text()').extract_first()
            item['tag'] = li.xpath('.//span[@class="inq"]/text()').extract_first()
            item['link'] = li.xpath('.//div[@class="hd"]/a/@href').extract_first()
            item['year'] = li.xpath('.//p[1]/text()[2]').extract_first().replace('\n','').replace(' ','')[:4]
            item['countries'] = li.xpath('.//p[1]/text()[2]').extract_first().replace('\n', '').replace(' ', '').replace('\xa0','')[4:].split('/')[1]


            # yield item


            yield scrapy.Request(
                item['link'],
                callback=self.parse_link,
                meta={"item":item}
            )


        #翻页链接
        next_link = 'https://movie.douban.com/top250'+response.xpath('//span[@class="next"]/a/@href').extract_first()
        if next_link is not None:
            yield scrapy.Request(
                next_link,
                callback=self.parse
            )

    def parse_link(self,response):
        item = response.meta["item"]
        item['director'] = response.xpath('//*[@id="info"]/span[1]/span[2]/a/text()').extract_first()
        # item['countries'] = response.xpath('//*[@id="info"]/text()').extract()
        # type = response.xpath('//span[@property="v:genre"]/text()').extract()
        item['type'] = ','.join(response.xpath('//span[@property="v:genre"]/text()').extract())

        yield item


